package com.hnichr.ping.tenant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hnichr.ping.common.vo.TenantResultVo;
import com.hnichr.ping.tenant.service.TenantService;
import com.hnichr.ping.tenant.entity.Tenant;
import com.hnichr.ping.tenant.entity.TenantBizType;
import com.hnichr.ping.tenant.mapper.TenantBizTypeMapper;
import com.hnichr.ping.tenant.mapper.TenantMapper;
import com.hnichr.ping.common.security.SecurityPair;
import com.hnichr.ping.common.security.SecurityUtils;
import com.hnichr.ping.common.vo.TenantAddVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class TenantServiceImpl implements TenantService {

    private static final Logger logger = LoggerFactory.getLogger(TenantServiceImpl.class);

    @Autowired
    private TenantMapper tenantMapper;

    @Autowired
    private TenantBizTypeMapper bizTypeMapper;


    @Override
    public TenantResultVo add(TenantAddVo vo) {
        LambdaQueryWrapper<Tenant> tenantQueryWrapper = new LambdaQueryWrapper<>();
        tenantQueryWrapper.eq(Tenant::getTenantCode,vo.getTenantCode());
        Tenant tenant = tenantMapper.selectOne(tenantQueryWrapper);
        if(tenant==null){
            return newAdd(vo);
        }
        else{
            if(!StringUtils.equals(tenant.getStatus(),"1")){
                return TenantResultVo.getFailed("租户已禁用！");
            }
            LambdaQueryWrapper<TenantBizType> bizTypeQueryWrapper = new LambdaQueryWrapper<>();
            bizTypeQueryWrapper.eq(TenantBizType::getTenantId,tenant.getId());
            List<TenantBizType> tenantBizTypeList = bizTypeMapper.selectList(bizTypeQueryWrapper);
            for(TenantBizType bizType:tenantBizTypeList){
                if(StringUtils.equals(bizType.getBizType(),vo.getMessageType())){
                    return TenantResultVo.getFailed("渠道已存在！");
                }
            }
            TenantBizType tenantBizType = new TenantBizType();
            tenantBizType.setTenantId(tenant.getId());
            tenantBizType.setBizType(vo.getMessageType());
            bizTypeMapper.insert(tenantBizType);
            TenantResultVo resultVo = TenantResultVo.getSuccess();
            resultVo.setTenantCode(vo.getTenantCode());
            resultVo.setTenantName(tenant.getTenantName());
            resultVo.setAppKey(tenant.getAppKey());
            resultVo.setAppSecret(tenant.getAppSecert());
            resultVo.setMessageType(vo.getMessageType());
            return resultVo;
        }
    }

    @Override
    public Tenant getTenantByAppKey(String appKey){
        LambdaQueryWrapper<Tenant> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Tenant::getAppKey,appKey);
        return tenantMapper.selectOne(queryWrapper);
    }

    private TenantResultVo newAdd(TenantAddVo vo){
        SecurityPair securityPair = SecurityUtils.getSecurityPair(vo.getTenantCode());
        Tenant tenant = new Tenant();
        tenant.setTenantCode(vo.getTenantCode());
        tenant.setTenantName(vo.getTenantName());
        tenant.setAppKey(securityPair.getAppKey());
        tenant.setAppSecert(securityPair.getAppSecret());
        tenant.setStatus("1");
        tenantMapper.insert(tenant);

        TenantBizType tenantBizType = new TenantBizType();
        tenantBizType.setTenantId(tenant.getId());
        tenantBizType.setBizType(vo.getMessageType());
        bizTypeMapper.insert(tenantBizType);
        TenantResultVo resultVo = TenantResultVo.getSuccess();
        resultVo.setTenantCode(vo.getTenantCode());
        resultVo.setTenantName(tenant.getTenantName());
        resultVo.setAppKey(tenant.getAppKey());
        resultVo.setAppSecret(tenant.getAppSecert());
        resultVo.setMessageType(vo.getMessageType());
        return resultVo;
    }
}
